package com.budwk.app.sms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.budwk.app.sms.dto.AliSmsMsgDTO;
import com.budwk.app.sms.service.AliSmsService;
import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.List;
import java.util.Map;

/**
 * @author ZY.Wang
 * @since 2024/3/13
 */
@IocBean
public class AliSmsServiceImpl implements AliSmsService {

    public Integer safeCheckOperatorSend(List<String> phoneList, String url) {
        AliSmsMsgDTO smsMsgDTO = new AliSmsMsgDTO();
        smsMsgDTO.setPhoneNumberSet(Sets.newHashSet(phoneList));
        smsMsgDTO.setTemplateCode("SMS_465406985");

        Map<String, String> templateParamMap = Maps.newHashMap();
        templateParamMap.put("url_code", url);
        smsMsgDTO.setTemplateParamMap(templateParamMap);
        return sendOneMessage(smsMsgDTO);
    }

    private Integer sendOneMessage(AliSmsMsgDTO msgDTO) {
        com.aliyun.dysmsapi20170525.Client client = createClient();
        if (client == null) return 0;

        String numbers = Joiner.on(",").join(msgDTO.getPhoneNumberSet());
        String signName = "";
        String templateParam = JSONObject.toJSONString(msgDTO.getTemplateParamMap());
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setPhoneNumbers(numbers)
                .setSignName(signName)
                .setTemplateCode(msgDTO.getTemplateCode())
                .setTemplateParam(templateParam);
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
            String s = sendSmsResponse.getBody().toString();
            System.out.println(s);
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }

        return 0;
    }

    private Client createClient() {
        try {
            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                    // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                    .setAccessKeyId("LTAI5tGYDzf12xw7LwP6X1fN")
                    // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                    .setAccessKeySecret("LTAI5tGYDzf12xw7LwP6X1fNVeLxm8G3Z3IT5YhwC8jgC2qLhejTrR");
            config.endpoint = "dysmsapi.aliyuncs.com";
            return new com.aliyun.dysmsapi20170525.Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
